﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using Utility;

public partial class Admin_Role_RoleManager : System.Web.UI.Page
{
    RoleManager dllRoleManager = new RoleManager();
    Model.UserInfo objUserInfo = new Model.UserInfo();
    DataTable _powerTable = new DataTable();
    Authentication dllAuthentication = new Authentication();
    protected void Page_Load(object sender, EventArgs e)
    {
        dllAuthentication.isAdminLogin();
        this.btnAdd.Enabled = false;
        _powerTable = dllAuthentication.getPower(objUserInfo.UserName, "角色管理");
        for (int i = 0; i < _powerTable.Rows.Count; i++)
        {
            if (_powerTable.Rows[i]["PowerName"].ToString() == "新建")
            {
                this.btnAdd.Enabled = true;
            }
            if (_powerTable.Rows[i]["PowerName"].ToString() == "删除")
            {
            }
            if (_powerTable.Rows[i]["PowerName"].ToString() == "查看")
            {
            }
            if (_powerTable.Rows[i]["PowerName"].ToString() == "修改")
            {
            }
        } 
        if (!IsPostBack)
        {
            BindGridView();
        }
    }

    /// <summary>
    /// 绑定GridView
    /// </summary>
    void BindGridView()
    {
        this.gdvRole.DataSource = dllRoleManager.get();
        this.gdvRole.DataBind();
    }

    /// <summary>
    /// 角色组添加
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2009-1-7</date>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (this.hId.Value.Length > 0)
        {
            object[] _value = new object[2];
            _value[0] = Convert.ToInt32(this.hId.Value);
            _value[1] = this.txtRoleName.Text.Trim();
            int result = dllRoleManager.edit(RoleManager.field.RoleId.ToString(), _value);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("修改权限未成功，请重试！");
            }
            else
            {
                Response.Redirect("RoleManager.aspx");
            }
        }
        else
        {

            object[] _value = new object[1];
            _value[0] = this.txtRoleName.Text;
            int result = dllRoleManager.add(_value);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("添加权限未成功，请重试！");
            }
            else
            {
                Response.Redirect("RoleManager.aspx");
            }
        }
    }

   

    



    /// <summary>
    /// 全选事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ckbAllSelect_CheckedChanged(object sender, EventArgs e)
    {
        if (this.ckbAllSelect.Checked == true)
        {
            for (int i = 0; i < this.gdvRole.Rows.Count; i++)
            {
                CheckBox ckb = (CheckBox)this.gdvRole.Rows[i].Cells[0].FindControl("ckbSelect");
                ckb.Checked = true;
            }
        }
        else
        {
            for (int i = 0; i < this.gdvRole.Rows.Count; i++)
            {
                CheckBox ckb = (CheckBox)this.gdvRole.Rows[i].Cells[0].FindControl("ckbSelect");
                ckb.Checked = false;
            }
        }
    }

    /// <summary>
    /// 新建事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAddRole_Click(object sender, EventArgs e)
    {
        this.hId.Value = "0";
        this.txtRoleName.Text = "";
    }


    /// <summary>
    /// 行创建事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvRole_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //鼠标进入行时
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#C0C0FF';this.style.cursor='hand';");
            //鼠标离开行时
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
            //任意行都为选择行
            e.Row.Attributes.Add("onclick", "__doPostBack('gdvRole','Select$" + e.Row.RowIndex + "')");
        }
    }

    /// <summary>
    /// 行选择事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvRole_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {        
        this.hId.Value = this.gdvRole.DataKeys[e.NewSelectedIndex]["RoleId"].ToString();
        this.txtRoleName.Text = this.gdvRole.Rows[e.NewSelectedIndex].Cells[2].Text;
        if (this.txtRoleName.Text == "&nbsp;")
        {
            this.txtRoleName.Text = "";
        }
    }


    /// <summary>
    /// 删除事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDel_Click1(object sender, EventArgs e)
    {
        for (int i = 0; i < this.gdvRole.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gdvRole.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked)
            {
                object[] _value = new object[1];
                _value[0] = Convert.ToInt32(this.gdvRole.DataKeys[i]["RoleId"].ToString());

                int result = dllRoleManager.del(RoleManager.field.RoleId.ToString(), _value);
                if (result < 0)
                {
                    Utility.Error.RedirectErrorPage("删除角色未成功，请重试！");
                }
                else
                {
                    Response.Redirect("RoleManager.aspx");
                }
            }
        }
    }

    /// <summary>
    /// 自动编号
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvRole_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            int id = e.Row.RowIndex + 1;
            e.Row.Cells[1].Text = id.ToString();
        }
    }
}
